<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
</head>
<body>
    <script >
      function * gen(arg){
         console.log(arg);
         let one = yield 111;
         console.log(one);//BBB
         let two = yield 222;
         console.log(two);//CCC
         let three = yield test();
         console.log(three);//DDD
      }

      function test(){
         return "带有返回值"
      }

      // 获得迭代器
      let iterator = gen("AAA");

      console.log(iterator.next());

      // next 方法可以传入实参
      // 将会作为 上一个 yield语句的返回结果
      console.log(iterator.next('BBB'));
      console.log(iterator.next('CCC'));
      console.log(iterator.next('DDD'));


      // 异步变成，文件操作，网络操作

      // 1s 后台控制台输出 111
      // 2s 后后输出22
      // 3s 后输出 33

      function one(){
         setTimeout(() => {
            console.log(111);
            // 通过这个来触发下一个
            g3.next();
         }, 1000);
      }

      function two(){
         setTimeout(() => {
            console.log(222);
            g3.next('8989');
         }, 2000);
      }

      function three(){
         setTimeout(() => {
            console.log(333);
            g3.next();
         }, 3000);
      }

      function * gen3(){
         yield one();
         yield two();
         yield three()
      }

      let g3 = gen3()
      g3.next()
      

   </script>
</body>

</html>